<?php
class PayUForm extends BaseForm {
    
    public function configure() {
        
        if ($this->getOption('CSRFProtection')===false) {
            $this->disableLocalCSRFProtection();    
        }
        
        $this->setWidgets(array(
          'first_name'   => new sfWidgetFormInputText(),
          'last_name'    => new sfWidgetFormInputText(),
          'email'        => new sfWidgetFormInputText(),
          'pay_type'     => new sfWidgetFormPlatnosci(),
          
          'pos_id'       => new sfWidgetFormInputHidden(),
          'pos_auth_key' => new sfWidgetFormInputHidden(),
          'session_id'   => new sfWidgetFormInputHidden(),
          'amount'       => new sfWidgetFormInputHidden(),
          'client_ip'    => new sfWidgetFormInputHidden(),
          'js'           => new sfWidgetFormInputHidden(),
          'desc'         => new sfWidgetFormInputHidden(),
          'ts'           => new sfWidgetFormInputHidden(),
          'sig'           => new sfWidgetFormInputHidden(),
          'rabat'       => new sfWidgetFormInputHidden()
            
        ));

        $this->setValidators(array(
          'first_name' => new sfValidatorString(array('max_length' => 255, 'required' => true)),
          'last_name'  => new sfValidatorString(array('max_length' => 255, 'required' => true)),
          'email'      => new sfValidatorEmail(array('required'=>true)),
          'client_ip'  => new sfValidatorString(array('required'=>true)),
          'pay_type'   => new sfValidatorString(array('required'=>true)),
          'js'         => new sfValidatorBoolean(array('required'=>true)),
          
          'pos_id'       => new sfValidatorString(array('required'=>true)),
          'pos_auth_key' => new sfValidatorString(array('required'=>true)),
          'session_id'   => new sfValidatorString(array('required'=>true)),
          'amount'       => new sfValidatorString(array('required'=>true)),
          'desc'         => new sfValidatorString(array('required'=>true)),
          'ts'           => new sfValidatorString(array('required'=>true)),
          'sig'          => new sfValidatorString(array('required'=>true)),
          'rabat'        => new sfValidatorPass()
        ));
        
        $this->widgetSchema->setLabels(array(
            'first_name' => 'Imię',
            'last_name'  => 'Nazwisko',
            'email'      => 'Email'
        ));
        
    }

    /**
    *  Ustawia formularz na podstawowe dane potrzebne do uiszczenia opłaty przez usera
    *  @param $user - class Pracownik
    */ 
    public function setForUser(sfGuardUser $user) {
        unset($this['pos_id'],$this['pos_auth_key'],$this['session_id'],$this['amount'],
            $this['desc'],$this['ts'],$this['sig']
        );
        $this->setDefaults(array(
            'first_name' => $user->getProfile()->getFirstName(),
            'last_name'  => $user->getProfile()->getLastName(),
            'email'      => $user->getEmail(),
            'client_ip'  => $_SERVER['REMOTE_ADDR'],
            'js'         => 0
        ));
        
        $this->widgetSchema->setNameFormat('platnosci[%s]');        
        
    }        
    
    /**
    *  Ustawia formularz na podstawowe dane potrzebne do uiszczenia opłaty przez usera
    *  @param $baseForm - Formularz wypełniony przez usera w pierwszym kroku
    *  @param $pos_id - kod pos_id nadana przez płatności.pl
    *  @param $pos_auth_key - kod pos_auth_key nadana przez płatności.pl
    *  @param $kwota - kwota do zapłaty w groszach
    *  @param $session_id - unikalny kod dla tranzakcji 
    *  @param $desc - dowolny opis transakcji
    *  @param $key_1 - klucz_1 z platnosci.pl do podpisywania formularza
    */    
    public function setPlatnosci(PayUForm $baseForm,$pos_id,$pos_auth_key,$kwota,$session_id,$desc,$key_1) {
        
        $this->widgetSchema['first_name'] = new sfWidgetFormInputHidden();
        $this->widgetSchema['last_name'] = new sfWidgetFormInputHidden();
        $this->widgetSchema['email'] = new sfWidgetFormInputHidden();
        $this->widgetSchema['pay_type'] = new sfWidgetFormInputHidden();
        $this->setDefaults(array(
            'first_name' => $baseForm->getValue('first_name'),
            'last_name'  => $baseForm->getValue('last_name'),
            'email'      => $baseForm->getValue('email'),
            'client_ip'  => $baseForm->getValue('client_ip') ? $baseForm->getValue('client_ip') : $_SERVER['REMOTE_ADDR'],
            'js'         => $baseForm->getValue('js'),
            'pay_type'   => $baseForm->getValue('pay_type'),
            'pos_id'         => $pos_id,
            'pos_auth_key'   => $pos_auth_key,
            'amount'         => $kwota,
            'session_id'     => $session_id,
            'desc'           => $desc,
            'ts'             => time(),
        ));
        $this->setDefault('sig',$this->calculateSignature($key_1));
    }
    
    private function calculateSignature($key_1) {
        $array = array('pos_id'=>'','pay_type'=>'','session_id'=>'','pos_auth_key'=>'','amount'=>'',
                        'desc'=>'','desc2'=>'','trsDesc'=>'','order_id'=>'','first_name'=>'',
                        'last_name'=>'','payback_login'=>'','street'=>'','street_hn'=>'','street_an'=>'',
                        'city'=>'','post_code'=>'','country'=>'','email'=>'','phone'=>'','language'=>'',
                        'client_ip'=>'','ts'=>'','key1'=>'');
        $defaults = $this->getDefaults();

        foreach($array as $key=>$value) {
            $array[$key] = array_key_exists($key,$defaults) ? $defaults[$key] : '';
        };
        $array['key1']=$key_1;

        return md5(implode('',$array));        
    }
}  

